package com.yst.gateway.monitor.controllers;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yst.gateway.monitor.common.beans.Response;
import com.yst.gateway.monitor.models.QzChannels;
import com.yst.gateway.monitor.services.QzChannelService;

@RestController
@RequestMapping(value = "/qz/channel")
public class QzChannelController {

	
	@Autowired
	private QzChannelService qzChannelService;
	
	/**
	 * 查询机构管理列表
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/getAll", method = RequestMethod.GET)
	public Response getAll(@RequestParam Map<String, Object> map) {
		Response response=new Response();
		response.success(qzChannelService.getByMap(map));
		return response;
	}
	
	/**
	 * 添加机构管理记录
	 * @param qzChannel
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Response add(@RequestBody Map<String, Object> map) {
		Response response = new Response();
		QzChannels qz = new QzChannels();
		qz.setName(map.get("name").toString());
		qz.setCode(map.get("code").toString());
		qz.setMemo(map.get("memo").toString());
		qz.setBusinessId(map.get("busid").toString());
		response.success(qzChannelService.add(qz));
		return response;
	}
	
	/**
	 * 机构管理详情
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	public Response get(@PathVariable("id") String id) {
		Response response = new Response();
		response.success(qzChannelService.getById(id));
		return response;
	}
	
	/**
	 * 机构管理修改
	 * @param qzChannel
	 * @return
	 */
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public Response update(@RequestBody Map<String, Object> map) {
		Response response = new Response();
		QzChannels qz = new QzChannels();
		qz.setChannelId(map.get("id").toString());
		qz.setName(map.get("name").toString());
		qz.setCode(map.get("code").toString());
		qz.setStatus(Integer.parseInt(map.get("status").toString()));
		qz.setMemo(map.get("memo").toString());
		qz.setBusinessId(map.get("busid").toString());
		response.success(qzChannelService.update(qz));
		return response;
	}
	
	/**
	 * 机构管理删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/del/{id}", method = RequestMethod.PUT)
	public Response del(@PathVariable("id") String id) {
		Response response = new Response();
		boolean flag = qzChannelService.delById(id);
		if(flag) {
			response.success(); 
		}else {
			response.failure("error");
		}
		return response;
	}
	
	/**
	 * 机构管理分页
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value="/getByPage", method=RequestMethod.POST)
	public Response getByPage(@RequestBody Map<String, Object> paramMap) {
		Response response = new Response();
		int pageNum=1;
		int pageSize=10;
		try{
			if(paramMap.containsKey("pageNum")){
				pageNum=(Integer)paramMap.get("pageNum");
				paramMap.remove("pageNum");
			}
			if(paramMap.containsKey("pageSize")){
				pageSize=(Integer)paramMap.get("pageSize");
				paramMap.remove("pageSize");
			}
		}catch (Exception e) {e.printStackTrace();
		}
		response.success(qzChannelService.getByPage(pageNum, pageSize, paramMap));
		return response;
	}
	
	/**
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/getBybusName", method = RequestMethod.POST)
	public Response getBybusName(@RequestBody Map<String, Object> map) {
		Response response = new Response();
		response.success(qzChannelService.getBybusName(map));
		return response;
	}
	
	/**
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/getRelateBusi/{id}", method = RequestMethod.GET) 
	public Response getRelateBusi(@PathVariable("id") String id) {
		Response response = new Response();
		response.success(qzChannelService.getRelateBusi(id));
		return response;		
	}
	
	/**
	 * check if name exists. if id is null, check all records,or check all except which id = parmId
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/checkName", method = RequestMethod.GET)
	public boolean checkName(@RequestParam Map<String, String> map) {
		return qzChannelService.checkName(map)==null?true:false;
	}
	
	/**
	 * check if code exists. if id is null, check all records,or check all except which id = parmId
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/checkCode", method = RequestMethod.GET)
	public boolean checkCode(@RequestParam Map<String, String> map) {
		return qzChannelService.checkCode(map)==null?true:false;
	}
}
